package com.onedec.materials.controller;

import com.onedec.materials.entity.OnedecMaterial;
import com.onedec.materials.entity.OnedecMaterialList;
import com.onedec.materials.service.MaterialService;
import com.onedec.utils.ResponseEntity;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.util.PerformanceSensitive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Log4j2
class MaterialsController {
    @Autowired
    private MaterialService materialService;
    /**
     * 添加材料
     */
    @GetMapping("inset")
    public ResponseEntity insetMaterial(@RequestBody  OnedecMaterial onedecMaterial){
        if(onedecMaterial!=null){
            int i = materialService.insetMaterial(onedecMaterial);
            if(i>0){
                return ResponseEntity.SUCCESS;
            }
        }
        return new ResponseEntity("添加失败，参数不能为空","404");
    }
    /**
     * 修改材料
     */
    @PostMapping("updateMaterials")
    public ResponseEntity updateMaterial(OnedecMaterial onedecMaterial){
        System.out.println("修改传进来的参数"+onedecMaterial);
        int i = materialService.updateMaterial(onedecMaterial);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }
        return new ResponseEntity("修改失败，请联系管理员","404");
    }
    /**
     * 查询所有材料
     */
    @GetMapping("selectAll")
    public ResponseEntity selectMaterial(){
        List<OnedecMaterial> list = materialService.selectAllMaterial();
        System.out.println("请求进来了");
        System.out.println(list);
        return new ResponseEntity(list);
    }

    /**
     * 通过id寻找
     * @param materialId
     * @return
     */
    @GetMapping("selectId")
    public ResponseEntity selectId(int materialId){
        OnedecMaterial material = materialService.selectId(materialId);
        if(material!=null){
            return new ResponseEntity(material);
        }
        return new ResponseEntity("还没有该商品，请添加后在查询","404");
    }

    /**
     * 通过id删除
     * @param materialId
     * @return
     */
    @GetMapping("deteleId")
    public ResponseEntity deleteMaterials(int materialId){
        int i = materialService.deleteMaterial(materialId);
        if(i>0){
            return ResponseEntity.SUCCESS;
        }
        return new ResponseEntity("删除失败","404");
    }
    /**
     * 根据名称及型号查找材料
     */
    @GetMapping("selectNameandModl")
    public ResponseEntity selectNameandModl(String materialModel,String materialName,String materialBak1,String materialBak2){
        Map<String,Object> map=new HashMap<>();
        map.put("materialModel",materialModel);
        map.put("materialName",materialName);
        map.put("materialBak1",materialBak1);
        map.put("materialBak2",materialBak2);
        System.out.println("map:"+map);
        List<OnedecMaterial> list = materialService.selectNameandModl(map);
        log.info("查询到的信息"+list);
        if(list!=null){
            return new ResponseEntity(list);
        }
        return new ResponseEntity("还没有你需要的材料哦，请添加后在查询！");
    }
    /**
     * 多表联查
     * 根据工程号查询需要的材料与工程名称
     */
    @GetMapping("selectIntable")
    public ResponseEntity selectInTable(int projectId,String projectCustomerName){
        System.out.println("传进来的参数："+projectId+"--------------------------"+projectCustomerName);
        Map<String, Object> map=new HashMap<>();
        map.put("projectId",projectId);
        map.put("projectCustomerName",projectCustomerName);
        List<Map<String, Object>> maps = materialService.selectInTable(map);
        return new ResponseEntity(maps);
    }
    /**
     * 查询材料与工程
     */
    @GetMapping("selectmAndp")
    public ResponseEntity selectmAndp(){
        List<Map<String, Object>> maps = materialService.selectmAndp();
        System.out.println(maps);
        return new ResponseEntity(maps);
    }
    /**
     * 查询项目材料
     */
    @GetMapping("findBymAndp")
    public ResponseEntity findBymAndp(int materialId){
        List<Map<String, Object>> list = materialService.findBymAndp(materialId);
        return new ResponseEntity(list);
    }
}
